<%-- 
    Document   : attribs
    Created on : May 10, 2014, 2:27:07 AM
    Author     : tadiwa
--%>

<%@page import="org.apache.shiro.subject.Subject"%>
<%@page import="org.apache.shiro.SecurityUtils"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.Hashtable"%>
<%@page import="javax.naming.directory.*,javax.naming.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>YAP</title>
    </head>
    <body>
        <h1>Y.A.P</h1>
        User: <%= SecurityUtils.getSubject().getPrincipal().toString()%>
        <br/>
        <%
            Subject s = SecurityUtils.getSubject();
            if (s.hasRole("admin")) {
                out.println("Role: Administrator");
            } else {
                out.println("Ordinary User");
            }
        %>
        <br/>
        <% out.println("---------------------------------------------------"); %><br/>
        <% out.println("PRIVILEGE ATTRIBUTE SERVER"); %><br/>
        <% out.println("---------------------------------------------------"); %>
        <br/>
        <%
            String str = request.getParameter("cntxt");
            Hashtable<String, Object> env = new Hashtable<String, Object>(11);
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://10.130.0.49:10389/o=yap");

            try {

                DirContext ctx = new InitialDirContext(env);
                Attributes answer = ctx.getAttributes("cn=" + SecurityUtils.getSubject().getPrincipal().toString() + ", ou=users");

                // Print the answer
                try {
                    for (NamingEnumeration ae = answer.getAll(); ae.hasMore();) {
                        Attribute attr = (Attribute) ae.next();
                        System.out.println("attribute: " + attr.getID());
                        out.println("attribute: " + attr.getID() + "<BR>");

                        /* print each value */
                        for (NamingEnumeration e = attr.getAll(); e.hasMore(); out.println("value: " + e.next()));
                        for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out.println("value: " + e.next()));
                        System.out.println("------------------------------------------------");
                        out.println("<BR>------------------------------------------------<BR>");
                    }
                } catch (NamingException e) {
                    e.printStackTrace();
                    System.out.println(e.getMessage());
                    out.println(e.getMessage());
                }
                //printAttrs(answer);

                // Close the context when we're done
                ctx.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                out.println(e.getMessage());
            }
        %>
        <br/>
        <a href="index.xhtml">Index</a>
    </body>
</html>
